#include "wxsstringproperty.h"
#include <globals.h>
#include <prep.h>

#define VALUE   wxsVARIABLE(Object,Offset,wxString)

wxsStringProperty::wxsStringProperty( const wxString& PGName, const wxString& DataName, long _Offset,
                                      bool _IsLongString, bool _XmlStoreEmpty, const wxString& _Default, int Priority ):
  wxsProperty( PGName, DataName, Priority ),
  Offset( _Offset ),
  IsLongString( _IsLongString ),
  XmlStoreEmpty( _XmlStoreEmpty ),
  Default( _Default ) {}

void wxsStringProperty::PGCreate( wxsPropertyContainer* Object, wxPropertyGridManager* Grid, wxPGId Parent ) {
  wxString Fixed = VALUE;
  Fixed.Replace( _T( "\n" ), _T( "\\n" ) );
  wxPGId Id;
  if( IsLongString ) {
    Id = Grid->AppendIn( Parent, new wxLongStringProperty( GetPGName(), wxPG_LABEL, Fixed ) );
  } else {
    Id = Grid->AppendIn( Parent, new wxStringProperty( GetPGName(), wxPG_LABEL, Fixed ) );
  }
  PGRegister( Object, Grid, Id );
}

bool wxsStringProperty::PGRead( M_Code_unused wxsPropertyContainer* Object, wxPropertyGridManager* Grid, wxPGId Id,
                                M_Code_unused long Index ) {
  VALUE = Grid->GetPropertyValue( Id ).GetString();
  VALUE.Replace( _T( "\\n" ), _T( "\n" ) );
  return true;
}

bool wxsStringProperty::PGWrite( M_Code_unused wxsPropertyContainer* Object, wxPropertyGridManager* Grid, wxPGId Id,
                                 M_Code_unused long Index ) {
  wxString Fixed = VALUE;
  Fixed.Replace( _T( "\n" ), _T( "\\n" ) );
  Grid->SetPropertyValue( Id, Fixed );
  return true;
}

bool wxsStringProperty::XmlRead( M_Code_unused wxsPropertyContainer* Object, TiXmlElement* Element ) {
  if( !Element ) {
    VALUE.Clear();
    return false;
  }
  wxString Base = cbC2U( Element->GetText() );
  wxString Result;
  for( const wxChar* Ch = Base.c_str(); *Ch; Ch++ ) {
    if( *Ch == _T( '_' ) ) {
      if( *++Ch == _T( '_' ) ) {
        Result << _T( '_' );
      } else
      { Result << _T( '&' ) << *Ch; }
    } else if( *Ch == _T( '\\' ) ) {
      switch( *++Ch ) {
        case _T( 'n' ):
          Result << _T( '\n' );
          break;
        case _T( 'r' ):
          Result << _T( '\r' );
          break;
        case _T( 't' ):
          Result << _T( '\t' );
          break;
        case _T( '\\' ):
          Result << _T( '\\' );
          break;
        default:
          Result << _T( '\\' ) << *Ch;
          break;
      }
    } else {
      Result << *Ch;
    }
  }
  VALUE = Result;
  return true;
}

bool wxsStringProperty::XmlWrite( M_Code_unused wxsPropertyContainer* Object, TiXmlElement* Element ) {
  if( XmlStoreEmpty || ( VALUE != Default ) ) {
    wxString Base = VALUE;
    wxString Result;
    for( const wxChar* Ch = Base.c_str(); *Ch; Ch++ ) {
      switch( *Ch ) {
        case _T( '_' ):
          Result << _T( "__" );
          break;
        case _T( '\\' ):
          Result << _T( "\\\\" );
          break;
        default:
          Result << *Ch;
      }
    }
    Element->InsertEndChild( TiXmlText( cbU2C( Result ) ) );
    return true;
  }
  return false;
}

bool wxsStringProperty::PropStreamRead( M_Code_unused wxsPropertyContainer* Object, wxsPropertyStream* Stream ) {
  return Stream->GetString( GetDataName(), VALUE, Default );
}

bool wxsStringProperty::PropStreamWrite( M_Code_unused wxsPropertyContainer* Object,
    wxsPropertyStream* Stream ) {
  return Stream->PutString( GetDataName(), VALUE, Default );
}
